package org.maplanning;

import org.maplanning.GridMap.StrategyI;

/**
 * Greedy strategy, moves just toward the closest goal state
 * @author Jaak Simm
 */
public class GreedyStrat implements StrategyI {

	@Override
	public char act(GridMap grid) {
		int[] closestGoal = null;
		double closestDist = Double.POSITIVE_INFINITY;
		
		// find the closest goal state:
		for (int[] goal : grid.goalStates) {
			double dist = GridMap.distanceL1(goal, grid.agent);
			if (dist < closestDist) {
				closestGoal = goal;
				closestDist = dist;
			}
		}
		
		// find neighbor closest to the closest goal:
		int[] closestNeighbor = null;
		closestDist = Double.POSITIVE_INFINITY;
		for (int[] neighbor : grid.getNeighbors(grid.agent)) {
			double dist = GridMap.distanceL1(closestGoal, neighbor);
			if (dist < closestDist) {
				closestNeighbor = neighbor;
				closestDist = dist;
			}
		}
		// no neighbors:
		if (closestNeighbor==null) {
			return 0;
		}
		closestNeighbor[0] -= grid.agent[0];
		closestNeighbor[1] -= grid.agent[1];
		return grid.getAction(closestNeighbor);
	}

}
